//
//  GoodsInfoHeaderView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/1.
//

#import "GoodsInfoHeaderView.h"

@interface GoodsInfoHeaderView ()

@property (nonatomic, strong) UIImageView *avatarImageV;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *originalLabel;

@end

@implementation GoodsInfoHeaderView

- (void)initViews {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.avatarImageV];
    [self addSubview:self.nameLabel];
    [self addSubview:self.timeLabel];
    [self addSubview:self.originalLabel];
    
    [self.avatarImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(40);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.avatarImageV.mas_trailing).offset(8);
        make.top.equalTo(self.avatarImageV);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel);
        make.bottom.equalTo(self.avatarImageV);
    }];
    [self.originalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-16);
        make.centerY.equalTo(self.avatarImageV);
    }];
}

- (void)setModel:(ShopModel *)model {
    [self.avatarImageV sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    self.nameLabel.text = model.nickName;
    self.timeLabel.text = [NSDate formateData:model.createdAt];
    self.originalLabel.hidden = model.parentUserName.length <= 0;
    if (model.parentUserName.length > 0) {
        NSString *str = [NSString stringWithFormat:@"商品来自于 %@",model.parentUserName];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange range = [str rangeOfString:model.parentUserName];
        [attr addAttributes:@{NSForegroundColorAttributeName:kBgColor} range:range];
        self.originalLabel.attributedText = attr;
    }
}

- (UIImageView *)avatarImageV {
    if (!_avatarImageV) {
        _avatarImageV = [[UIImageView alloc] init];
        _avatarImageV.contentMode = UIViewContentModeScaleAspectFill;
        _avatarImageV.layer.cornerRadius = 20;
        _avatarImageV.layer.masksToBounds = YES;
    }
    return _avatarImageV;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = kRGB(0x333333);
        _nameLabel.font = kFontMediium(14);
        [_nameLabel sizeToFit];
    }
    return _nameLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.textColor = kRGB(0x999999);
        _timeLabel.font = kFontRegula(10);
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}
- (UILabel *)originalLabel {
    if (!_originalLabel) {
        _originalLabel = [[UILabel alloc] init];
        _originalLabel.font = kFontRegula(12);
        _originalLabel.textColor = kRGB(0x666666);
        [_originalLabel sizeToFit];
    }
    return _originalLabel;
}

@end
